﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SafetyRecord
{
    public partial class SoundRecord
    {
        [DllImport("WinMm.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

        private const int ERROR_SUCCESS = 0;
        private const string MODE_UNKNOWN = "unknown";

        private static bool mciSendString(string strCommand)
        {
            //return mciSendString(strCommand, null, 0, 0) != ERROR_SUCCESS;
            return false;
        }
    }

    public partial class SoundRecord
    {
        private int m_channels;
        private int m_sample_spersec;
        private string m_format_tag;
        private int m_bits_per_sample;

        public SoundRecord()
        {
            this.Channels = 2;
            this.FormatTag = "pcm";
            this.BitsPerSample = 8;
            this.SampleSpersec = 11025;
        }
        // 采样位数
        public virtual int BitsPerSample
        {
            set
            {
                if (mciSendString("set wave bitpersample " + value))
                    this.m_bits_per_sample = value;
            }
            get
            {
                return this.m_bits_per_sample;
            }
        }
        // 采样频率
        public virtual int SampleSpersec
        {
            get
            {
                return this.m_sample_spersec;
            }
            set
            {
                if (mciSendString("set wave samplespersec " + value))
                    this.m_sample_spersec = value;
            }
        }
        // 声道
        public virtual int Channels
        {
            get
            {
                return m_channels;
            }
            set
            {
                if (mciSendString("set wave channels " + value))
                    this.m_channels = value;
            }
        }
        // 格式标签
        public virtual string FormatTag
        {
            get
            {
                return this.m_format_tag;
            }
            set
            {
                if (mciSendString("set wave format tag " + value))
                    this.m_format_tag = value;
            }
        }
        // 打开录音
        public virtual bool OpenRecord()
        {
            return mciSendString("open new type waveaudio alias movie");
        }
        // 开始录音
        public virtual bool StartRecord()
        {
            return mciSendString("record movie");
        }
        // 停止录音
        public virtual bool StopRecord()
        {
            return mciSendString("stop movie");
        }
        // 保存录音
        public virtual bool SaveRecord(string saveFileName)
        {
            return mciSendString("save movie " + saveFileName);
        }
        // 关闭录音
        public virtual bool CloseRecord()
        {
            return mciSendString("close movie");
        }
        // 暂停录音
        public virtual bool PauseRecord()
        {
            return mciSendString("pause movie");
        }
        // 恢复录音
        public virtual bool ResumeRecord()
        {
            return mciSendString("resume movie");
        }
        // 执行状态
        public virtual string Status
        {
            get
            {
                string strBuffer = new string('\0', 12);
                if (mciSendString("status movie mode", strBuffer, 12, 0) != ERROR_SUCCESS)
                    return MODE_UNKNOWN;
                if ((strBuffer = strBuffer.Remove(strBuffer.IndexOf('\0'))).Length <= 0)
                    return MODE_UNKNOWN;
                return strBuffer;
            }
        }
    }
}
